package com.atguigu.controller;

import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;
import com.atguigu.utils.ViewBaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName: javaweb
 *
 * @author: 赵伟风
 * time: 2022/7/2 14:17 周六
 * description: 用户登录的controller
 */
@WebServlet("/user/login")
public class UserLoginController extends ViewBaseServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //2.获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //调用业务层代码
        UserService userService = new UserServiceImpl();
        //登录成功还是失败
        boolean flag = userService.login(username, password);

        //6.前端数据响应 【4选1  转发  重定向】
        if (flag){
            //登录成功
            processTemplate("index",req,resp);
        }else{
            //登录失败
            processTemplate("login",req,resp);
        }

    }

}
